<?php
include_once "header.php";


$cek_field="";

if (ISSET($_POST["rekam_pengguna"])){

	if($_POST['nip']!="" && $_POST['pass1']!="" && $_POST['pass2']!="" && $_POST['nama']!="" && $_POST['email']!="" && $_POST['status']!=""){
			$user = new User();
			$user->setNip($_POST['nip']);
			$user->setPassword($_POST['pass1']);
			$user->setNama($_POST['nama']);
			$user->setEmail($_POST['email']);
			$user->setAktif($_POST['status']);
			
			$daftar_user = new Daftar_user();
			
			if($daftar_user->add($user)==true){
				echo "<script>
						alert('Informasi: Data berhasil direkam');
						window.location.href='daftar_pengguna.php';
					</script>";
			} else {
			
				echo "<script>
						alert('Informasi: Data gagal direkam');
					</script>";
			
				
			}
		
		} 
}
?>


<div id="container">
	<div id="errorcont">
		<?php echo $cek_field;
		?>
	</div>
	<div id="maincont">
		<?php include_once "menu_samping.php"; ?>
		<div id="mainright">
			<div id="title">
			Rekam Data Pengguna
			</div>
			<div id="body">
			</br>
			<form name="form_rekam_pengguna" method="POST" action="rekam_pengguna.php">
			<input type="hidden" name="rekam_pengguna">
			<table border=0 cellspacing=0 cellpadding=4 width=100%>		
				<tr>
					<td>NIP</td>
					<td>:</td>
					<td>
					<input type="text" name="nip" maxlength="18">
					</td>
				</tr>
				<tr>
					<td>Nama</td>
					<td>:</td>
					<td>
					<input type="text" name="nama" maxlength="64">
					</td>
				</tr>
				
				<tr>
					<td>Password</td>
					<td>:</td>
					<td>
					<input type="password" name="pass1" maxlength="32">
					</td>
				</tr>
				
				<tr>
					<td>Konfirmasi Password</td>
					<td>:</td>
					<td>
					<input type="password" name="pass2" maxlength="32">
					</td>
				</tr>
				<tr>
					<td>Email</td>
					<td>:</td>
					<td>
					<input type="text" name="email" maxlength="32">
					</td>
				</tr>
				<tr>
					<td>Status</td>
					<td>:</td>
					<td>
					<select name="status">
						<option value="1" selected >Aktif</option>
						<option value="0" >Tidak Aktif</option>
					
					<select>
					</td>
				</tr>
				<tr>
					<td></td>
					<td></td>
					<td align="right">
					
					<input type="submit" value="Simpan" onclick="return cekField();"> 
					<input type="button" value="Batal" onClick="window.location.href='daftar_pengguna.php'">
					
					</td>
				</tr>
	
			</table>
			<br>
			
			</form>
						
			</div>
        </div>
	</div>
</div>
<?php
	include_once "footer.php";
?>

<script type="text/javascript">
function cekField(){
	
		var nip = document.forms['form_rekam_pengguna']['nip'].value;
		var nama = document.forms['form_rekam_pengguna']['nama'].value;
		var pass1 = document.forms['form_rekam_pengguna']['pass1'].value;
		var pass2 = document.forms['form_rekam_pengguna']['pass2'].value;
		var email = document.forms['form_rekam_pengguna']['email'].value;
		var status = document.forms['form_rekam_pengguna']['status'].value;
		var angka = /^\d{18}$/;
		
		var at = email.indexOf("@");
		var dot = email.lastIndexOf(".");
		
		
		if(nip=="" || nama=="" || pass1=="" || pass2=="" || email=="" || status==""){
			alert('Peringatan : Silahkan isikan seluruh data yang tersedia.');
			return false;
		}
		
		if (angka.test(nip)==false){
		  alert ('Peringatan: Silahkan input nip dengan 18 digit angka.');
		  return false;
		}
		
		if (pass1!=pass2){
		  alert ('Peringatan: Input password tidak sama.');
		  return false;
		}
		
		if(at<1 || dot<at+2 || dot+2>email.length){
			alert('Peringatan : Silahkan isikan format email dengan benar.');
			return false;
		}

}
	
</script>

